/**
 * Created by George on 2018/2/23.
 */

import util from "../common/util"

// 将本控制器函数作为结果返回给router.js
var editUserCtrl = function ($scope, $http, $stateParams, $state) {
    $scope.welcome_info = "会员管理";
    $scope.user = {
        id: "",
        name: "",
        info: "",
        joinDate: util.getNowFormatDate()
    };
    $scope.$emit('updateRoute', 'user');

    $scope.editUser = function () {
        var data = {
            "id": $scope.user.id,
            "name": $scope.user.name,
            "info": $scope.user.info,
            "joinDate": $scope.user.joinDate
        };
        $http.put('/basketballfire/rest/user', data).then(function (req) {
            console.log("success!");
            alert("修改成功！");
            $state.go("user");
        }, function (req) {
            console.log("failed!");
            alert("修改失败！");
        });
    };

    $scope.cancel = function () {
        window.location.href = "/basketballfire/index.html#/user";
    };

    function initOrUpdateData() {
        $scope.user = {
            id: "",
            name: "",
            info: "",
            joinDate: util.getNowFormatDate()
        };

        var userID = $stateParams.userId;

        $http.get("/basketballfire/rest/user?id=" + userID).then(function (data, status) {
            console.log(data);
            var usersData = data.data;
            $scope.user = {
                id: usersData.id,
                name: usersData.name,
                info: usersData.info,
                joinDate: usersData.joinDate
            };
        });
    }

    function init() {
        initOrUpdateData();
    }

    //ctrl start
    init();
};

export default editUserCtrl;